﻿using BusinessLayer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using BusinessLayer.BLServerReference;
namespace WebFrontEnd.Services
{
    /// <summary>
    /// Summary description for EventsService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class EventsService : System.Web.Services.WebService
    {
        #region Private Members
        private IBL _bl;
        private User _currentUser;
        private JavaScriptSerializer _json;
        #endregion

        #region Private Properties
        private IBL BL
        {
            //Lazy Loading
            get
            {
                if (_bl == null)
                {
                    _bl = BusinessLayerFactory.GetCurrentBusinessLayer(ConfigurationManager.AppSettings.Get("DomainName"));
                }
                return _bl;
            }
        }

        private User CurrentUser
        {
            //Lazy Loading
            get
            {
                if (_currentUser == null)
                {
                    _currentUser = GetCurrentUser();
                }
                return _currentUser;
            }
        }

        private JavaScriptSerializer JSON
        {
            get
            {
                if (_json == null)
                {
                    _json = new JavaScriptSerializer();
                }
                return _json;
            }
        }
        #endregion

        #region Private Methods
        protected User GetCurrentUser()
        {
            return BL.GetUserByEmail(User.Identity.Name);
        }
        #endregion

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetEventReport(int eventID)
        {
            var ev = BL.GetEventById(eventID);
            if (ev.UserID != CurrentUser.ID)
            {
                return "";
            }
            EventReport report = BL.GetEventReport(ev);
            report.Event.User = null;
            return JSON.Serialize(report);
        }
    }
}
